package com.mmall.controller.portal;

import com.mmall.common.IConst;
import com.mmall.common.ResponseInfo;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICartService;
import com.mmall.vo.CartVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 购物车控制层
 * Created by qezhhnjy on 2017/10/1-16:04.
 */
@Controller
@RequestMapping("/cart/")
public class CartController {

    @Resource
    private ICartService iCartService;

    /**
     * 添加指定数量的指定商品到用户购物车，并返回完整的购物车信息
     *
     * @param session   　当前用户
     * @param count     　购买商品数量
     * @param productId 　商品id
     * @return 包含完整购物车信息的响应对象
     */
    @ResponseBody
    @RequestMapping(value = "add.do", method = RequestMethod.POST)
    public ServerResponse<CartVo> add(HttpSession session, Integer count, Integer productId) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iCartService.add(user.getId(), productId, count);
    }

    /**
     * 更新购物车信息
     *
     * @param session   　当前会话
     * @param count     　商品数量
     * @param productId 　商品id
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    public ServerResponse<CartVo> update(HttpSession session, Integer count, Integer productId) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iCartService.update(user.getId(), productId, count);
    }

    /**
     * 删除指定一个或多个购物车
     *
     * @param session    　当前会话
     * @param productIds 　多个商品id的字符串，每个id间用','分割。
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "delete.do", method = RequestMethod.POST)
    public ServerResponse<CartVo> delete(HttpSession session, String productIds) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iCartService.delete(user.getId(), productIds);
    }

    /**
     * 查询指定用户购物车列表
     *
     * @param session 当前用户
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "list.do", method = RequestMethod.POST)
    public ServerResponse<CartVo> list(HttpSession session) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iCartService.list(user.getId());
    }

    @RequestMapping(value = "list2.do", method = RequestMethod.POST)
    public String list2(HttpSession session, Model model) {
        System.out.println("-----------------");
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return "cart";
        ServerResponse<CartVo> list = iCartService.list(user.getId());
        model.addAttribute("cart", list.getData());
        return "cart";
    }

    //全选
    @ResponseBody
    @RequestMapping(value = "select_all.do", method = RequestMethod.POST)
    public ServerResponse<CartVo> selectAll(HttpSession session) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iCartService.selectORNot(user.getId(), IConst.CartConst.CHECKED, null);
    }

    //全反选
    @ResponseBody
    @RequestMapping(value = "select_null.do", method = RequestMethod.POST)
    public ServerResponse<CartVo> selectNull(HttpSession session) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iCartService.selectORNot(user.getId(), IConst.CartConst.UN_CHECKED, null);
    }

    //单独反选
    @ResponseBody
    @RequestMapping(value = "select_not.do", method = RequestMethod.POST)
    public ServerResponse<CartVo> selectNot(HttpSession session, Integer productId) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iCartService.selectORNot(user.getId(), IConst.CartConst.UN_CHECKED, productId);
    }

    //单独选
    @ResponseBody
    @RequestMapping(value = "select_one.do", method = RequestMethod.POST)
    public ServerResponse<CartVo> selectOne(HttpSession session, Integer productId) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), ResponseInfo.NEED_LOGIN.getDescription());
        return iCartService.selectORNot(user.getId(), IConst.CartConst.CHECKED, productId);
    }

    //查询当前用户的购物车里面的产品数量,用于反馈在首页购物车图标上方.
    @ResponseBody
    @RequestMapping(value = "get_cart_product_count.do", method = RequestMethod.POST)
    public ServerResponse<Integer> getCartProductCount(HttpSession session) {
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createBySuccess(0);
        return iCartService.getCartProductCount(user.getId());
    }

}
